/////////////////////////////////////////////////////////////////////////////
// Name:        SettingsDialog.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     04/01/2009 18:57:59
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "SettingsDialog.h"
#include "mirrorslst.h"
#include "mounttable.h"
#include "config.h"

////@begin XPM images
////@end XPM images


/*!
 * SettingsDialog type definition
 */

IMPLEMENT_DYNAMIC_CLASS( SettingsDialog, wxDialog )


/*!
 * SettingsDialog event table definition
 */

BEGIN_EVENT_TABLE( SettingsDialog, wxDialog )

////@begin SettingsDialog event table entries
    EVT_WINDOW_DESTROY( SettingsDialog::OnDestroy )

    EVT_TREE_SEL_CHANGED( ID_MIRRORS_TREE, SettingsDialog::OnMIRRORSTREESelChanged )

////@end SettingsDialog event table entries

END_EVENT_TABLE()


/*!
 * SettingsDialog constructors
 */

SettingsDialog::SettingsDialog()
{
    Init();
}

SettingsDialog::SettingsDialog( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
}


/*!
 * Settings creator
 */

bool SettingsDialog::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin SettingsDialog creation
    SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY|wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );

    CreateControls();
    Centre();
////@end SettingsDialog creation
    return true;
}


/*!
 * SettingsDialog destructor
 */

SettingsDialog::~SettingsDialog()
{
////@begin SettingsDialog destruction
////@end SettingsDialog destruction
}


/*!
 * Member initialisation
 */

void SettingsDialog::Init()
{
////@begin SettingsDialog member initialisation
    p_mirrors_tree = NULL;
    p_mirror_text = NULL;
    p_mount_info_static = NULL;
////@end SettingsDialog member initialisation
}


/*!
 * Control creation for Settings
 */

void SettingsDialog::CreateControls()
{    
////@begin SettingsDialog content construction
    SettingsDialog* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    wxNotebook* itemNotebook3 = new wxNotebook( itemDialog1, ID_NOTEBOOK, wxDefaultPosition, wxDefaultSize, wxBK_DEFAULT );

    wxPanel* itemPanel4 = new wxPanel( itemNotebook3, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxTAB_TRAVERSAL );
    wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxVERTICAL);
    itemPanel4->SetSizer(itemBoxSizer5);

    p_mirrors_tree = new wxTreeCtrl( itemPanel4, ID_MIRRORS_TREE, wxDefaultPosition, itemPanel4->ConvertDialogToPixels(wxSize(240, 150)), wxTR_HAS_BUTTONS |wxTR_TWIST_BUTTONS|wxTR_FULL_ROW_HIGHLIGHT|wxTR_LINES_AT_ROOT|wxTR_HIDE_ROOT|wxTR_ROW_LINES|wxTR_HAS_VARIABLE_ROW_HEIGHT|wxTR_SINGLE|wxSUNKEN_BORDER );
    itemBoxSizer5->Add(p_mirrors_tree, 1, wxGROW|wxALL, 0);

    wxStaticText* itemStaticText7 = new wxStaticText( itemPanel4, wxID_STATIC, _("Select a mirror from mirrors list or input\ncustom mirror location, ie:\n  *  http://any.web-server.com/cygwin/\n  *  ftp://any.ftp-server.com/cygwin/\n  *  \\\\lan-share\\cygwin\n  *  D:\\download\\cygwin"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticText7->SetFont(wxFont(9, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Courier New")));
    itemBoxSizer5->Add(itemStaticText7, 0, wxGROW|wxALL, 5);

    wxBoxSizer* itemBoxSizer8 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer5->Add(itemBoxSizer8, 0, wxGROW|wxALL, 0);
    wxStaticText* itemStaticText9 = new wxStaticText( itemPanel4, wxID_STATIC, _("Location :"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer8->Add(itemStaticText9, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_mirror_text = new wxTextCtrl( itemPanel4, ID_MIRROR_TEXT, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer8->Add(p_mirror_text, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    itemNotebook3->AddPage(itemPanel4, _("Tab"));

    wxPanel* itemPanel11 = new wxPanel( itemNotebook3, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxTAB_TRAVERSAL );
    wxBoxSizer* itemBoxSizer12 = new wxBoxSizer(wxVERTICAL);
    itemPanel11->SetSizer(itemBoxSizer12);

    p_mount_info_static = new wxStaticText( itemPanel11, wxID_STATIC, _("Static text"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer12->Add(p_mount_info_static, 1, wxGROW|wxALL, 5);

    itemNotebook3->AddPage(itemPanel11, _("Tab"));

    itemBoxSizer2->Add(itemNotebook3, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer14 = new wxStdDialogButtonSizer;

    itemBoxSizer2->Add(itemStdDialogButtonSizer14, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
    wxButton* itemButton15 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer14->AddButton(itemButton15);

    wxButton* itemButton16 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer14->AddButton(itemButton16);

    itemStdDialogButtonSizer14->Realize();

    // Connect events and objects
    itemDialog1->Connect(ID_SETTINGS_DLG, wxEVT_DESTROY, wxWindowDestroyEventHandler(SettingsDialog::OnDestroy), NULL, this);
////@end SettingsDialog content construction
    
	MirrorsList& mirrors_lst = MirrorsList::get();
	mirrors_lst.reload_mirrors();
	
    const wxTreeItemId& root = p_mirrors_tree->AddRoot(_T("ROOT"));
	
	const wxArrayString& continents = mirrors_lst.get_continents();
    for( int i=0; i<continents.GetCount(); i++ ) {
    	// add continent nodes
        const wxTreeItemId& id = p_mirrors_tree->AppendItem(root, continents[i]);
        
        const wxArrayString& countries = mirrors_lst.get_countries(continents[i]);
        
        for (int i=0; i<countries.GetCount(); i++) {
        	// add country nodes
        	const wxTreeItemId& id2 = p_mirrors_tree->AppendItem(id, countries[i]);
        	
        	const wxArrayString& mirrors = mirrors_lst.get_mirrors(countries[i]);
        	
            for (int j=0; j<mirrors.GetCount(); j++) {
            	// add mirror nodes
            	p_mirrors_tree->AppendItem(id2, mirrors[j] );
        	}
    	}
    }
    
    p_mirrors_tree->ExpandAll();
    
    // Show mount table info
    MountTable& mount_table = MountTable::get();
    MountPointHash mount_hash = mount_table.mount_hash;
    
    wxString info;
    MountPointHash::iterator it;
    for( it = mount_hash.begin(); it != mount_hash.end(); ++it ) {
    	info.Append(wxString::Format(_T("%s, %s\n"), it->first.c_str(), it->second.native.c_str()));
    }
    
    p_mount_info_static->SetLabel(info);
}


/*!
 * Should we show tooltips?
 */

bool SettingsDialog::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap SettingsDialog::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin SettingsDialog bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end SettingsDialog bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon SettingsDialog::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin SettingsDialog icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end SettingsDialog icon retrieval
}


/*!
 * wxEVT_COMMAND_TREE_SEL_CHANGED event handler for ID_MIRRORS_TREE
 */

void SettingsDialog::OnMIRRORSTREESelChanged( wxTreeEvent& event )
{
	const wxString& name = p_mirrors_tree->GetItemText(event.GetItem());
	
	wxString url = MirrorsList::get().get_url(name);
	
	p_mirror_text->SetValue(url);
}

/*!
 * wxEVT_DESTROY event handler for ID_SETTINGS_DLG
 */

void SettingsDialog::OnDestroy( wxWindowDestroyEvent& event )
{
    event.Skip();
	MirrorsList& mirrors_lst = MirrorsList::get();
	Config& config = Config::get();
    
    if ( p_mirror_text->GetValue().IsEmpty() ) {
    	const wxArrayString& mirrors = mirrors_lst.get_mirrors();
    	
    	// gen random number
    	srand(time(0));
    	int r = rand() % mirrors.GetCount();
    	
    	config.set_last_mirror(mirrors[r]);
    }
}

